package com.wu.project.admin.service.impl;

import com.wu.project.admin.component.JsonResult;
import com.wu.project.admin.dao.AuthorityDao;
import com.wu.project.admin.entity.AuthorityMenuDTO;
import com.wu.project.admin.entity.UpdateRoleVO;
import com.wu.project.admin.service.AuthorityService;
import com.wu.project.admin.util.CommonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuan
 */
@Service
public class AuthorityServiceImpl implements AuthorityService {

    @Autowired
    AuthorityDao authorityDao;

    @Override
    public JsonResult getAuthUserList() {
        return CommonResultUtil.Instance.success(authorityDao.queryUserList());
    }

    @Override
    public JsonResult getAuthorityByUserId(String userId) {
        List<AuthorityMenuDTO> authorityMenuDTOList = authorityDao.queryMenu(userId);
        if (authorityMenuDTOList.size() > 0) {
            AuthorityMenuDTO info = authorityMenuDTOList.get(0);
            Map<String, Object> result = new HashMap<>(2);
            result.put("menu", authorityMenuDTOList);
            result.put("roleName", info.getRoleName());
            result.put("roleZhName", info.getRoleExplain());
            result.put("userRoleId", info.getUserRoleId());
            result.put("roleId", info.getUserId());
            return CommonResultUtil.Instance.success(result);
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult getRoleList() {
        List<Map<String, Object>> roleList = authorityDao.queryRole();
        return CommonResultUtil.Instance.success(roleList);
    }

    @Override
    public JsonResult getRoleMenu(Long roleId) {
        return CommonResultUtil.Instance.success(authorityDao.queryRoleMenuList(roleId));
    }

    @Override
    public JsonResult deleteRoleMenu(Long roleMenuId) {
        Integer state = authorityDao.deleteRoleMenu(roleMenuId);
        if (state > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult updateRole(UpdateRoleVO updateRoleVO) {
        if (updateRoleVO.getUserRoleId() == null) {
            List<Map<String, Object>> userRoleList = authorityDao.queryUserRole(updateRoleVO);
            if (userRoleList.size() > 0) {
                Integer state = authorityDao.updateUserRole(updateRoleVO);
                if (state > 0) {
                    return CommonResultUtil.Instance.success();
                }
            } else {
                Integer integer = authorityDao.addUserRole(updateRoleVO);
                if (integer > 0) {
                    return CommonResultUtil.Instance.success();
                }
            }
        } else {
            Integer state = authorityDao.updateUserRole(updateRoleVO);
            if (state > 0) {
                return CommonResultUtil.Instance.success();
            }
        }
        return CommonResultUtil.Instance.error();
    }


}
